package com.itheima.sys.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.itheima.common.entity.MyAuthority;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 用户表 系统-用户表
 * </p>
 *
 * @author itheima
 * @since 2020-12-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SysUserDTO implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    private Long id;

    /**
     * 姓名
     */
    private String username;

    /**
     * 密码 密码
     */
    private String password;

    /**
     * 手机号 手机号
     */
    private String mobile;

    /**
     * 微信
     */
    private String wxAccount;

    /**
     * 邮箱
     */
    private String email;

    /**
     * QQ
     */
    private String qqAccount;

    /**
     * 主体颜色
     */
    private String color;

    /**
     * 状态 1激活、2未激活、3注销
     */
    private Integer status;

    /**
     * 创建时间 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 最后登录公司id
     */
    private Long lastLoginCompanyId;

    private List<SysRoleDTO> roles;  //用户角色

    private List<SysFunctionDTO> functions;  //用户权限

    private List<MyAuthority> grantedAuthorities;

    /**
     * 封装当前用户所有的角色 以及 权限
     * @return
     */
    @Override
    public List<MyAuthority> getAuthorities() {
        grantedAuthorities = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roles)){
            grantedAuthorities.addAll(roles);
        }
        if(!CollectionUtils.isEmpty(functions)){
            grantedAuthorities.addAll(functions);
        }
        return grantedAuthorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
